﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SequenceComplexElementSerialization", Namespace="http://kis.x-road.eu/", Target=typeof(SequenceComplexElementSerialization))]
    public class SequenceComplexElementSerializationSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected SequenceComplexElementSerialization clazz;
        
        public SequenceComplexElementSerializationSerializer(SequenceComplexElementSerialization clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("EmployerNil");
            if (clazz.EmployerNil!=null) {
                global::Xtee.Core.SerializationMetadata metaEmployerNil = this.GetMetaData(clazz.EmployerNil);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Employee), clazz.EmployerNil.GetType())) {
                    Writer.WriteTypeAttribute(metaEmployerNil.TypeQualifiedName.Namespace, metaEmployerNil.TypeQualifiedName.Name);
                }
                var employerNilSerializer = metaEmployerNil.NewSerializer(clazz.EmployerNil);
                employerNilSerializer.WriteXmlAttributes(Writer);
                employerNilSerializer.WriteXml(Writer);
            }
            else {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("EmployerNonNil");
            if (clazz.EmployerNonNil!=null) {
                global::Xtee.Core.SerializationMetadata metaEmployerNonNil = this.GetMetaData(clazz.EmployerNonNil);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Employee), clazz.EmployerNonNil.GetType())) {
                    Writer.WriteTypeAttribute(metaEmployerNonNil.TypeQualifiedName.Namespace, metaEmployerNonNil.TypeQualifiedName.Name);
                }
                var employerNonNilSerializer = metaEmployerNonNil.NewSerializer(clazz.EmployerNonNil);
                employerNonNilSerializer.WriteXmlAttributes(Writer);
                employerNonNilSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : EmployerNonNil");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="EmployerNil") {
                if ((Reader.IsElementNil() == false)) {
                    global::Xtee.Core.SerializationMetadata metaEmployerNil = this.GetMetaData<global::Kis.Xroad.Eu.Employee>(Reader, "Employee", "http://kis.x-road.eu/");
                    clazz.EmployerNil = metaEmployerNil.NewEntity<global::Kis.Xroad.Eu.Employee>();
                    if ((Reader.IsEmptyElement == false)) {
                        var employerNilSerializer = metaEmployerNil.NewSerializer(clazz.EmployerNil);
                        employerNilSerializer.ReadXmlAttributes(Reader);
                        Reader.ReadStartElement("EmployerNil");
                        employerNilSerializer.ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceComplexElementSerialization>EmployerNil", Reader.LocalName);
            }
            if (Reader.LocalName=="EmployerNonNil") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceComplexElementSerialization>EmployerNonNil");
                }
                global::Xtee.Core.SerializationMetadata metaEmployerNonNil = this.GetMetaData<global::Kis.Xroad.Eu.Employee>(Reader, "Employee", "http://kis.x-road.eu/");
                clazz.EmployerNonNil = metaEmployerNonNil.NewEntity<global::Kis.Xroad.Eu.Employee>();
                if ((Reader.IsEmptyElement == false)) {
                    var employerNonNilSerializer = metaEmployerNonNil.NewSerializer(clazz.EmployerNonNil);
                    employerNonNilSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("EmployerNonNil");
                    employerNonNilSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceComplexElementSerialization>EmployerNonNil", Reader.LocalName);
            }
        }
    }
}
